// function getName(a) {
//   return function(b) {
//     return a+b
//   }
// }
// let add = getName(100)
// console.log(add(2)) //3
// console.log(add(3)) //4
// function Person() {
//   this.name = 1
// }
// // Person.prototype = 2
// Object.prototype.age = 2
// console.log(Person.prototype.__proto__)
// let p1 = new Person()
// console.log(p1)
// console.log(p1.__proto__===Person.prototype)
// console.log(p1.name, p1.age)
//obj.__proto__  ===> obj.prototype,内部有__proto__ ===> obj.prototype的构造函数的原型对象
// p1内部找到了name属性，但是没找到age，顺着__proto__指向p1构造函数Person的原型对象（Person.prototype）找age属性，Person.prototype里面也没找到age属性
//Person.prototype也是一个对象，里面也有__proto__指向Person.prototype对象的构造函数的原型对象（Object.prototype）,找到了age属性
// let obj ={
//   name:222
// }
// console.log(obj.age)
// console.log(Object.prototype.__proto__)

console.log(1)
new Promise((resolve) => {
  console.log(5)
  resolve()
}).then(() => {//微任务
  console.log(6)
})
Promise.resolve().then(() => {// 微任务
  console.log(2)
  setTimeout(() => {
    console.log(3)
  }, 0)
})

setTimeout(() => {//宏任务
  Promise.resolve().then(() => { //微任务队列加一个任务
    console.log(8)
  })
  console.log(4)
}, 0)

// console.log(7)
// console.log("AAA")
// setTimeout(() => console.log("BBB"), 1000)

// const start = new Date()
// while(new Date() - start < 3000) {

// }
// console.log('CCC')
// setTimeout(() => console.log("DDD"), 0)
// new Promise(() => {
//   console.log('EEE')
//   foo.bar(100)
// }).then(() => console.log("FFF"))
// .then(res => console.log("GGG"))
// .catch(err => {console.log('HHH')})
// console.log("III");  
//1, 5, 7

//微任务：6, 2
//宏任务：4
//微任务：8
//宏任务：3

// AAA/CCC/EEE/III/HHH/DDD/BBB
// async function fun() {
//   async function async1() {
//     console.log("AAAA");
//     async2();
//     console.log("BBBB");
//   }
//   async function async2() {
//     console.log("CCCC");
//   }
//   console.log("DDDD");
//   setTimeout(function () {
//     console.log("FFFF");
//   }, 0);
//   await async1();
//   new Promise(function (resolve) {
//     console.log("GGGG");
//   });
// }
// fun()
//栈
function StackFunc(value) {
  this.value = value
  this.length = 0
  this.push = (value) => {
    this.value.unshift(value)
    this.length+=1
    console.log(this.value)
  }
  this.pop = () => {
    this.value.shift()
    this.length-=1
    console.log(this.value)
  }
  this.peek = () => {
    return this.length>0?this.value[0]:null
  }
  this.size = () => {
    return this.length
  }
} 
// let s1 = new StackFunc([1,2,3])
// console.log(s1)
// s1.push(100)
// s1.push(105)
// console.log(s1)
// s1.pop()
// console.log('__________________________________')

class Stack {
  constructor(value) {
    this.value = value
    this.getValue = () => {
      console.log(this.value)
    }
  }
  push(value) {
    this.value.unshift(value)
    this.length+=1
  }
  pop() {
    this.value.shift()
    this.length-=1
    console.log(this.value)
  }
  peek() {
    return this.length>0?this.value[0]:null
  }
  size() {
    return this.length
  }
} 
// let s2 = new Stack([1,2,3])
// console.log(s2)
// s2.push(100)
// s2.push(105)
// console.log(s2)
// s2.push()

// //队列
// class Queue {
//   constructor(){
//     this.value = []
//   }
//   enqueue(val) {
//     this.value.push(val)
//   }
//   dequeue() {
//     this.value.shift()
//   }
//   size() {
//     return this.value.length
//   }
// }
// function Queue() {
//   this.value = []
// }
// Queue.prototype.push = function(val) {
//   this.value.push(val)
// }

//DACBGF

class LinkListNodeClass{
  constructor(data) {
    this.data = data
    this.next = null
  }
}

class LinkListClass {
  constructor() {
    this.head = null
    this.length = 0
  }
  append(data) {
    let node = new LinkListNodeClass(data)
    if(this.length===0) {
      this.head = node
      this.length+=1
    } else {
      let current = this.head
      while(current.next) {
        current = current.next
      }
      current.next = node
      this.length+=1
    }
  }
  insert(data, position) {
    let node = new LinkListNodeClass(data)
    if(position <0 || position>this.length) return false
    if(position===0) {
      node.next = this.head
      this.head = node
    } else {
      let current = this.head
      for (let index = 0; index < position-1; index++) {
        current = current.next
      }
      node.next = current.next
      current.next = node
    }
    this.length += 1
  }
  get(position) {
    if(position <0 || position>this.length) return false
    // if(position===0) return this.head
    let current = this.head
    for (let index = 0; index < position; index++) {
      current = current.next  
    }
    return current
  }
  delete(position) {
    if(position <0 || position>this.length) return false
    let current = this.head
    if(position===0) {
      this.head = this.head.next
    } else {
      for (let index = 0; index < position-1; index++) {
        current = current.next
      }
      current.next = current.next.next
    }
    this.length-=1
    return this.head
  }
}
//      
// head 【「data」「next」】(next) =>【「data」「next」】(next) => 【「data」「next」】(next)】
let list1 = new LinkListClass()
list1.append(2)
list1.append(3)
list1.append(4)
list1.append(5)
list1.insert(100, 3)
console.log(list1.get(3))
console.log(list1)
console.log(list1.delete(3))
console.log(list1)


// function LinkListNode(data) {
//   this.data = data;
//   this.next = null;
// }
// //链表
// function LinkList() {
//   this.head = null;
//   this.length = 0;    // 记录链表节点的个数
// }
// LinkList.prototype.append = function(data){
//   var node = new LinkListNode(data)
//   console.log(node)
//   if(this.length===0) {
//     this.head = node
//   } else {
//     var current = this.head
//     while(current.next) {
//       current = current.next
//     }
//     current.next = node
//   }
//   this.length += 1
// }
// LinkList.prototype.insert = function(position, data){
//   var node = new LinkListNode(data)
//   if(position<0||position>this.length) return false
//   if(position===0) {
//     node.next = this.head
//     this.head = node
//   } else {
//     var current = this.head
//     for(let i = 0;i<position-1;i++) {
//       current = current.next
//     }
//     node.next = current.next
//     current.next = node
//   }
//   this.length +=1
// }
// LinkList.prototype.get = function(position) {
//   if(position<0||position>this.length) return null
//   let current = this.head
//   for (let index = 0; index < position; index++) {
//     current = current.next
//   }
//   return current
// }
// LinkList.prototype.delete = function(position) {
//   if(position<0||position>=this.length) return false
//   let current = this.head
//   if(position===0) this.head = this.head.next
//   else {
//     for(let i =0;i< position - 1;i++) {
//       current = current.next
//     }
//     current.next = current.next.next
//   }
//   this.length -= 1
//   return current
// }
// let l1 = new LinkList()
// console.log(l1)
// l1.append(1)
// l1.append(2)
// console.log(l1)





